package com.action.ui.fragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.action.childbase.ChildBaseFragment;
import com.action.entity.Assets;
import com.action.entity.event.SwitchAssetEvent;
import com.base.app_core.View.xrecyclerview.RecyclerAdapter;
import com.base.app_core.View.xrecyclerview.RecyclerViewHolder;
import com.base.app_core.service.pressenter.XYBUPressenter;
import com.base.app_core.service.user.model.User;
import com.base.app_core.service.user.model.UserAccount;
import com.base.app_core.util.AppConstants;
import com.blankj.utilcode.util.StringUtils;
import com.xinglai.yun.R;
import com.hwangjr.rxbus.RxBus;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class AssetFragment extends ChildBaseFragment<XYBUPressenter> {
    @BindView(R.id.data_container)
    XRecyclerView dataContainer;
    private AssetsAdapter assetsAdapter = null;
    @Inject
    public User user;
    @Inject
    UserAccount userAccount;

    DecimalFormat df = new DecimalFormat("######0.000");
    private List<Assets> mCompanys = new ArrayList<>();

    private void packData() {
        Assets company = new Assets();
        company.setTableKey(StringUtils.isEmpty(userAccount.getTableKey()) ? AppConstants.Const.DEFAULT_TABLE : userAccount.getTableKey());
        company.setTableName(StringUtils.isEmpty(userAccount.getTableName()) ? AppConstants.Const.DEFAULT_TABLE : userAccount.getTableName());
        double hper = 0;
        if (userAccount != null && !StringUtils.isEmpty(userAccount.getHistoryIncrease())) {
            try {
                hper = Double.parseDouble(userAccount.getHistoryIncrease());
                hper *= 100;
            } catch (Exception e) {

            }
        }

        company.setHistroyPercent(",历史+" + df.format(hper) + "%");

        double tper = 0;
        if (!StringUtils.isEmpty(userAccount.getTodayIncrease())) {
            try {
                tper = Double.parseDouble(userAccount.getTodayIncrease());
                tper *= 100;
            } catch (Exception e) {

            }
        }
        company.setTodayPercent("今日+" + df.format(tper) + "%");
        company.setAccount(userAccount.getAccount());
        mCompanys.add(company);
        if (null != assetsAdapter)
            assetsAdapter.notifyDataSetChanged(mCompanys);
    }

    public static class AssetsAdapter extends RecyclerAdapter<Assets, AssetsAdapter.ViewHolder> {

        private UserAccount userAccount = null;

        public void setUser(UserAccount userAccount) {
            this.userAccount = userAccount;
        }

        @Override
        protected void fillData(final Assets item, ViewHolder holder) {
            holder.companyTablename.setText(item.getTableName());
            holder.companyTodayPercent.setText(item.getTodayPercent());
            holder.companyHistoryPercent.setText(item.getHistroyPercent());
            holder.companyAccount.setText(item.getAccount());
            holder.companySwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (item.getTableKey().equals(AppConstants.Const.DEFAULT_TABLE) || item.getTableKey().equals(userAccount.getTableKey())) {
                        //直接跳转
                        RxBus.get().post(new SwitchAssetEvent());
                    } else {//切换
                        //todo 调用切换资产盘的api
                        RxBus.get().post(new SwitchAssetEvent());
                    }
                }
            });
        }

        @Override
        protected int getItemLayoutId() {
            return R.layout.unit_assets;
        }


        public class ViewHolder extends RecyclerViewHolder {

            @BindView(R.id.company_tablename)
            TextView companyTablename;
            @BindView(R.id.viewTag)
            TextView viewTag;
            @BindView(R.id.company_today_percent)
            TextView companyTodayPercent;
            @BindView(R.id.company_account)
            TextView companyAccount;
            @BindView(R.id.company_switch)
            TextView companySwitch;
            @BindView(R.id.company_history_percent)
            TextView companyHistoryPercent;

            public ViewHolder(View itemView) {
                super(itemView);
            }
        }
    }

    public AssetFragment() {
        // Required empty public constructor
    }

    @Override
    protected boolean isNeedShowLoading() {
        return false;
    }

    @Override
    protected boolean needShowError() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_baselist;
    }

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        assetsAdapter = new AssetsAdapter();
        assetsAdapter.setUser(userAccount);
        dataContainer.setAdapter(assetsAdapter);
        dataContainer.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        dataContainer.setBackgroundColor(getActivity().getResources().getColor(R.color.background_grey));
        packData();
    }
}
